package com.aplink.generic.util;

import com.aplink.dellcommercial.R;

import java.lang.reflect.Field;
import java.util.HashMap;

public final class AndroidUIParser {
    private static HashMap<String, Integer> sResourceIdMap;
    public static LogUtil mLogUtil = new LogUtil( "AndroidResourceIdHelper" );
    public static final int RESOURCE_NOT_FOUND = - 1;

    private static int getResourceIdByName( String resourceName,
                                            Class<?> resourceType ) {
        if ( sResourceIdMap.containsKey( resourceName ) ) {
            return sResourceIdMap.get( resourceName );
        }

        try {
            final Field field = resourceType.getField( resourceName );
            final Integer fieldId = field.getInt( field );
            sResourceIdMap.put( resourceName, fieldId );
            return fieldId;
        } catch ( final Exception ex ) {
            final String errorMessage = resourceName + " not found";
            mLogUtil.error( errorMessage, "getResourceIdByName", ex );
        }
        return RESOURCE_NOT_FOUND;
    }

    public static int getDrawableIdByName( String drawableName ) {
        return getResourceIdByName( drawableName, R.drawable.class );
    }

    public static int getColorIdByName( String colorName ) {
        return getResourceIdByName( colorName, R.color.class );
    }

    public static int getLayoutIdByName( String layoutName ) {
        return getResourceIdByName( layoutName, R.layout.class );
    }

    public static int getMenuIdByName( String menuName ) {
        return getResourceIdByName( menuName, R.menu.class );
    }

    public static int getRawIdByName( String rawName ) {
        return getResourceIdByName( rawName, R.raw.class );
    }

    public static int getStringIdByName( String stringName ) {
        return getResourceIdByName( stringName, R.string.class );
    }

    public static int getViewIdByName( String viewName ) {
        return getResourceIdByName( viewName, R.id.class );
    }

    static {
        if ( sResourceIdMap == null ) {
            sResourceIdMap = new HashMap<String, Integer>();
        }
    }

    private AndroidUIParser() {

    }
}
